﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SJ.Platform.Common;
using SJ.Platform.EFCore.DBOperation.MySQL.Project;
using SJ.Platform.EFCore.Model;
using SJ.Platform.EFCore.ModelExtend.ProjectExt;
using SJ.Platform.Extend.IOC.CurrentUser;
using SJ.Platform.Extend.Request;
using SJ.Platform.Extend.Response;

namespace SJ.Platform.Api.Controllers.ProjectManager
{
    /// <summary>
    /// 项目变更控制类
    /// </summary>
    [Route(GlobalVariableHandle.Scope_Api + "/ProjectChange")]
    [ApiController]
    [Authorize(GlobalVariableHandle.Scope_Api)]
    public class ProjectChangeController : ControllerBase
    {
        private readonly SJDBContext _context;
        private readonly ICurrentUser _currentUser;
        private readonly Project_Change_MainHandle _changeMainHandle;
        

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbContext">数据库</param>
        /// <param name="currentUser">当前用户</param>
        public ProjectChangeController(SJDBContext dbContext, ICurrentUser currentUser)
        {
            _context = dbContext;
            _currentUser = currentUser;
            _changeMainHandle = new Project_Change_MainHandle(dbContext);
        }

        /// <summary>
        /// 获取所有项目变更
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("GetProjectChangesAsync")]
        public async Task<IActionResult> GetProjectChangesAsync([FromBody] ApiPageQueryRequest<ProjectChangeMainExtend> request)
        {
            try
            {
                var result = await _changeMainHandle.GetProjectChangeMainsAsync((int)request.PageIndex, (int)request.PageSize, _currentUser.UserId, ApiPageQueryRequest<ProjectChangeMainExtend>.GenerateCondition(request.Condition));

                return Ok(ApiPageQueryResponse<List<ProjectChangeMainExtend>>.SetResponse(ApiResponseStatus.Success, "查询成功", (int)request.PageIndex, (int)request.PageSize, result.count, result.mainExtends)); // 返回成功的响应
            }
            catch (Exception ex)
            {
                // 处理异常情况
                return BadRequest(ApiPageQueryResponse<List<ProjectChangeMainExtend>>.SetResponse(ApiResponseStatus.BadRequest, "查询失败：" + ex.Message, (int)request.PageIndex, (int)request.PageSize, 0, new List<ProjectChangeMainExtend> { }));
            }
        }
    }
}
